package org.agilelang.generator.webservices.php;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


import java.util.List;
import org.agilelang.object.attributes.MenuItem;
import org.agilelang.mvc.Generator;
import org.agilelang.mvc.IMenu;
import org.agilelang.object.Menu;

public class GMenu extends Generator implements IMenu {

    public GMenu(String appName, String path, List<Menu> menus) {
        this.menus = menus;
        this.path = path;
        this.appName = appName;
        this.template = "bootstrap";
        setTemplate2("webservices/php");
    }

    @Override
    public void process() {
        String _menu = readStreamFiles("webservices/php", "_menu.tpl");
        String _menu_simple = readStreamFiles("webservices/php", "_menu_simple.tpl");
        String _menuitem = readStreamFiles("webservices/php", "_menuitem.tpl");
        int t = menus.size();
        String menu_main = "";

        for (int i = 0; i < t; i++) {
            Menu menu = menus.get(i);
            String action = "";
            if (menu.menuitems.get(0).action.equals("index") || menu.menuitems.get(0).action.equals("crud")) {
                action = "";
            } else {
                action = "/" + menu.menuitems.get(0).action;
            }

            int tt = menu.menuitems.size();
            if (tt == 0) {
                String aux_menu = _menu_simple;
                aux_menu = aux_menu.replace("//menu_caption//", menu.caption);
                aux_menu = aux_menu.replace("//menu_controller//", menu.menuitems.get(0).controller);
                aux_menu = aux_menu.replace("//menu_action//", action.toLowerCase());
                menu_main += aux_menu;
            } else {
                String aux_menu = _menu;
                aux_menu = aux_menu.replace("//menu_caption//", menu.caption);
                aux_menu = aux_menu.replace("//menu_controller//", menu.menuitems.get(0).controller);
                aux_menu = aux_menu.replace("//menu_action//", action.toLowerCase());

                String menu_tab = "";
                for (int ii = 0; ii < tt; ii++) {
                    MenuItem menuitem = menu.menuitems.get(ii);
                    String action_item = "";
                    String aux_menuitem = _menuitem;
                    if (menuitem.action.equals("index")) {
                        action_item = "";
                    } else if (menuitem.action.equals("crud")) {
                        action_item = "";
                    } else {
                        action_item = "/" + menuitem.action;
                    }

                    aux_menuitem = aux_menuitem.replace("//menu_caption//", menuitem.caption);
                    aux_menuitem = aux_menuitem.replace("//menu_controller//", menuitem.webclient.toLowerCase());
                    aux_menuitem = aux_menuitem.replace("//menu_action//", action_item);
                    menu_tab += aux_menuitem;
                }
                aux_menu = aux_menu.replace("<!-- menuitem -->", menu_tab);
                menu_main += aux_menu;
            }
        }
        String file = readStreamFiles("webservices/php", "header.php");
        file = file.replace("<!-- menu -->", menu_main);
        createfile("/webparts/header.php", path, file);
    }
}
